var CURRENT_URL = window.location.href.split('#')[0].split('?')[0],
    $BODY = $('body'),
    $SIDEBAR_MENU = $('#sidebar-menu'),
    $SIDEBAR_FOOTER = $('.sidebar-footer'),
    $LEFT_COL = $('.left_col'),
    $RIGHT_COL = $('.right_col'),
    $NAV_MENU = $('.nav_menu'),
    $FOOTER = $('footer');



$(function ()
{
	func_init_smart_wizard();
	func_init_daterangepicker();



	//=============== 列表复选框 =========================
	$('.checkbox-all').click(function ()
	{
		var checked = this.checked;

		$('.checkbox-one').each(function (index, elem)
		{
			elem.checked = checked;
		});
	});
	//=============== 列表复选框=========================



	//================分页=============================
	if ($(".aspnet-pager").text().trim().length <= 0)
	{
		$(".aspnet-pager").hide();
	}
	//================分页=============================
});


////////////////////////////
//// 初始化菜单点击事件
function func_init_sidebar()
{
	var setContentHeight = function ()
	{
		// reset height
		$RIGHT_COL.css('min-height', $(window).height());

		var bodyHeight = $BODY.outerHeight(),
			footerHeight = $BODY.hasClass('footer_fixed') ? -10 : $FOOTER.height(),
			leftColHeight = $LEFT_COL.eq(1).height() + $SIDEBAR_FOOTER.height(),
			contentHeight = bodyHeight < leftColHeight ? leftColHeight : bodyHeight;

		// normalize content
		contentHeight -= $NAV_MENU.height() + footerHeight;
		console.log("window.height = " + $(window).height());
		$RIGHT_COL.css('min-height', $(window).height());
	};

	$SIDEBAR_MENU.find('a').on('click', function (ev)
	{
		console.log('clicked - sidebar_menu');
		var $li = $(this).parent();

		if ($li.is('.active'))
		{
			$li.removeClass('active active-sm');
			$('ul:first', $li).slideUp(function ()
			{
				setContentHeight();
			});
		}
		else
		{
			// prevent closing menu if we are on child menu
			if (!$li.parent().is('.child_menu'))
			{
				$SIDEBAR_MENU.find('li').removeClass('active active-sm');
				$SIDEBAR_MENU.find('li ul').slideUp();
			} else
			{
				if ($BODY.is(".nav-sm"))
				{
					$SIDEBAR_MENU.find("li").removeClass("active active-sm");
					$SIDEBAR_MENU.find("li ul").slideUp();
				}
			}
			$li.addClass('active');

			$('ul:first', $li).slideDown(function ()
			{
				setContentHeight();
			});
		}
	});


	//隐藏显示菜单的小按钮
	$('#menu_toggle').click(function ()
	{
		console.log('clicked - menu toggle');

		if ($BODY.hasClass('nav-md'))
		{
			$SIDEBAR_MENU.find('li.active ul').hide();
			$SIDEBAR_MENU.find('li.active').addClass('active-sm').removeClass('active');
		}
		else
		{
			$SIDEBAR_MENU.find('li.active-sm ul').show();
			$SIDEBAR_MENU.find('li.active-sm').addClass('active').removeClass('active-sm');
		}

		$BODY.toggleClass('nav-md nav-sm');

		setContentHeight();
	});
};


////////////////////////////
//// 初始化编辑JS控件
function func_init_smart_wizard()
{
	if (typeof ($.fn.smartWizard) == 'undefined') { return; }
	//console.log('init_SmartWizard');

	//横模式
	$('#wizard').smartWizard({
		labelNext: "下一步",
		labelPrevious: "上一步",
		labelFinish: "保存",
		enableFinishButton: true,
		enableAllSteps: true,
		onFinish: function ()
		{
			//console.log("func_init_smart_wizard:onFinish");
			$("#ContentPlaceHolder_right_compSave").click();
		}
	});

	//竖模式
	//$('#wizard_verticle').smartWizard({
	//	transitionEffect: "slide",
	//});

	$(".actionBar").css({
		"text-align": "left"
	});


	$('.buttonNext').addClass('btn btn-primary');
	$('.buttonPrevious').addClass('btn btn-default');
	$('.buttonFinish').addClass('btn btn-success');


	var step_count = $(".wizard_steps li").length;
	//console.log("func_init_smart_wizard:step_count" + step_count);

	if (step_count <= 1)
	{
		$(".wizard_steps").hide();
		$('.buttonNext').hide();
		$('.buttonPrevious').hide();
	}


	if ($("#ContentPlaceHolder_right_compSave").hasClass("wizard-hide"))
	{
		$(".actionBar").hide();
	}
};



////////////////////////////
//// 初始化日历控件
function func_init_daterangepicker()
{
	if (typeof ($.fn.daterangepicker) == 'undefined') { return; }


	$('.daterangepicker_set').daterangepicker({
		singleDatePicker: true,
		showDropdowns: true,
		autoApply: true,
		locale: {
			format: 'YYYY-MM-DD',
			applyLabel: "确定",
			cancelLabel: "取消",
			resetLabel: "重置",
		},
	}, function (start, end, label)
	{
		console.log(start.format('YYYY-MM-DD'), end.format('YYYY-MM-DD'), label);

		var id = this.element.context.id;

		func_daterangepicker_set__success(start, end, id);
	});


	$('.daterangepicker_set_info').daterangepicker({
		singleDatePicker: true,
		timePicker: true,
		timePicker24Hour: true,
		timePickerIncrement: 5,
		autoApply: true,
		locale: {
			format: 'YYYY-MM-DD HH:mm',
			applyLabel: "确定",
			cancelLabel: "取消",
			resetLabel: "重置",
		},
	}, function (start, end, label)
	{
		console.log(start.format('YYYY-MM-DD'), end.format('YYYY-MM-DD'), label);

		var id = this.element.context.id;

		func_daterangepicker_set__success(start, end, id);
	});
};

function func_daterangepicker_set__success(start, end, id)
{
}


//////////////////////////////////////////////////
////// 加载中
function func_loading(is_hide, content)
{
	if ($("#loading").length > 0)
	{
		console.log("loading 存在");
	}
	else
	{
		var div = '<div id="loading" style="position: fixed; z-index: 1000; top: 0; right: 0; left: 0; bottom: 0; display: none;">'
			+ '<div style="position: fixed; width: 200px; padding: 10px 5px 0px 5px; z-index: 5000; top: 180px; left: 50%; margin-left: -100px; background: rgba(17, 17, 17, 0.7); text-align: center; border-radius: 5px; color: #FFFFFF;">'
			+ '<div class="progress progress-striped active">'
			+ '<div class="progress-bar progress-bar-info" style="width: 100%;"></div>'
			+ '</div>'
			+ '<h5 class="loading_content">正在加载</h5>'
			+ '</div></div>';

		$("body").prepend(div);
	}

	if (content)
	{
		$(".loading_content").text(content);
	}


	if (is_hide)
	{
		$("#loading").hide();
	}
	else
	{
		$("#loading").show();
	}
}

function func_alert(msg)
{
	if ($("#compAlert").length > 0)
	{
		console.log("alert 存在");
	}
	else
	{
		var div = `
	<div id="compAlert" class="modal fade bs-example-modal-lg" tabindex="-1">
		<div class="modal-dialog modal-sm" role="document">
			<div class="modal-content">
				<div class="modal-body">
					<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
					<h5><b class="modal-text">保存成功</b></h5>
				</div>
			</div>
		</div>
	</div>`

		$("body").prepend(div);
	}


	$("#compAlert .modal-body .modal-text").text(msg);
	$("#compAlert").modal("show");
}